package visiopuce.ui;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import visiopuce.objets.Alteration;
import visiopuce.service.AlterationServiceImpl;

public class CNVVerifie extends Dialog {

	private Button btnOk;
	private Button btnAnnuler;
	String titre;
	private String question;
	private List<Alteration> alts;
	private org.eclipse.swt.widgets.List comboCNV;
	private int idPuce;

	public CNVVerifie(Shell parentShell, String titre, String question, int idPuce) {
		super(parentShell);
		this.titre = titre;

		this.idPuce = idPuce;
		this.question = question;
		setShellStyle(SWT.CLOSE | SWT.TITLE | SWT.BORDER | SWT.APPLICATION_MODAL);
		setBlockOnOpen(true);
	}

	@Override
	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		shell.setText(titre);
	}

	@Override
	protected Control createDialogArea(Composite parent) {

		Composite container = (Composite) super.createDialogArea(parent);
		GridLayout layout = new GridLayout(2, false);
		layout.marginRight = 5;
		layout.marginLeft = 10;
		container.setLayout(layout);

		Label lblQuestion = new Label(container, SWT.NONE);
		lblQuestion.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblQuestion.setText(question);

		comboCNV = new org.eclipse.swt.widgets.List(container, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
		List<Alteration> cnvs = null;
		if (titre.contains("FISH")) {
			cnvs = AlterationServiceImpl.getInstance().getAlterationAVerifFISH(idPuce);
		}
		if (titre.contains("qPCR")) {
			cnvs = AlterationServiceImpl.getInstance().getAlterationAVerifqPCR(idPuce);
		}
		for (Alteration cnv : cnvs) {
			comboCNV.add("CNV : " + "Chr" + cnv.getChromosome() + ":" + cnv.getDebutHg19() + ".." + cnv.getFinHg19() + "N°" + cnv.getIdAlteration());
		}

		return container;
	}

	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, "Ok", true);
		createButton(parent, IDialogConstants.CANCEL_ID, "Annuler", false);
	}

	@Override
	protected Point getInitialSize() {
		return new Point(255, 198);
	}

	@Override
	protected void okPressed() {
		alts = new ArrayList<Alteration>();
		String[] selections = comboCNV.getSelection();
		for (String string : selections) {
			Integer idCNV = Integer.parseInt(string.substring(string.indexOf("°") + 1).trim());
			Alteration a = AlterationServiceImpl.getInstance().getAlterationById(idCNV);
			if (a != null) {
				alts.add(a);
			}
		}
		if (alts.size() == 0) {
			MessageDialog.openError(Display.getCurrent().getActiveShell(), "Champs obligatoire manquant", "Au moins un CNV doit être selectionné");
		} else {
			super.okPressed();
		}
	}

	public List<Alteration> getCNV() {
		return alts;
	}

}